package kav.util.logging;

import java.util.ResourceBundle;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

/**
 * @author Alexander V. Kashirsky (c) 2010.
 * @since kav-logging 1.0
 * @version kav-logging 1.0
 */
final class Log4jLogger extends kav.util.logging.Logger
{
    private static final String FQCN = Log4jLogger.class.getName();

    private final Logger log;

    private ResourceBundle resourceBundle;

    public Log4jLogger( Class< ? > clazz )
    {
        this.log = Logger.getLogger( clazz );
    }

    public Log4jLogger( String name )
    {
        this.log = Logger.getLogger( name );
    }

    @Override
    public void debug( Object message )
    {
        if ( this.log.isEnabledFor( Level.DEBUG ) )
            this.log.log( FQCN, Level.DEBUG, message, null );
    }

    @Override
    public void debug( Object message, Object ... params )
    {
        if ( this.log.isEnabledFor( Level.DEBUG ) )
        {
            String msg = String.format( message.toString(), params );
            this.log.log( FQCN, Level.DEBUG, msg, null );
        }
    }

    @Override
    public void debug( Object message, Throwable t )
    {
        if ( this.log.isEnabledFor( Level.DEBUG ) )
            this.log.log( FQCN, Level.DEBUG, message, t );
    }

    @Override
    public void debug( Object message, Throwable t, Object ... params )
    {
        if ( this.log.isEnabledFor( Level.DEBUG ) )
        {
            String msg = String.format( message.toString(), params );
            this.log.log( FQCN, Level.DEBUG, msg, t );
        }
    }

    @Override
    public void debugl7( String key )
    {
        if ( this.log.isEnabledFor( Level.DEBUG ) )
        {
            String message = null;

            if ( ( message = this.resourceBundle.getString( key ) ) != null )
            {
                this.log.log( FQCN, Level.DEBUG, message, null );
            }
        }
    }

    @Override
    public void debugl7( String key, Object ... params )
    {
        if ( this.log.isEnabledFor( Level.DEBUG ) )
        {
            String message = null;

            if ( ( message = this.resourceBundle.getString( key ) ) != null )
            {
                String msg = String.format( message, params );
                this.log.log( FQCN, Level.DEBUG, msg, null );
            }
        }
    }

    @Override
    public void debugl7( String key, Throwable t )
    {
        if ( this.log.isEnabledFor( Level.DEBUG ) )
        {
            String message = null;

            if ( ( message = this.resourceBundle.getString( key ) ) != null )
            {
                this.log.log( FQCN, Level.DEBUG, message, t );
            }
        }
    }

    @Override
    public void debugl7( String key, Throwable t, Object ... params )
    {
        if ( this.log.isEnabledFor( Level.DEBUG ) )
        {
            String message = null;

            if ( ( message = this.resourceBundle.getString( key ) ) != null )
            {
                String msg = String.format( message, params );
                this.log.log( FQCN, Level.DEBUG, msg, t );
            }
        }
    }

    /*
     * (non-Javadoc)
     * @see kav.util.logging.Logger#trace(java.lang.Object)
     */
    @Override
    public void trace( Object message )
    {
        if ( this.log.isEnabledFor( Level.TRACE ) )
        {
            this.log.log( FQCN, Level.TRACE, message, null );
        }
    }

    /*
     * (non-Javadoc)
     * @see kav.util.logging.Logger#trace(java.lang.Object, java.lang.Object[])
     */
    @Override
    public void trace( Object message, Object ... params )
    {
        if ( this.log.isEnabledFor( Level.TRACE ) )
        {
            String msg = String.format( message.toString(), params );
            this.log.log( FQCN, Level.TRACE, msg, null );
        }
    }

    /*
     * (non-Javadoc)
     * @see kav.util.logging.Logger#trace(java.lang.Object, java.lang.Throwable)
     */
    @Override
    public void trace( Object message, Throwable t )
    {
        if ( this.log.isEnabledFor( Level.TRACE ) )
            this.log.log( FQCN, Level.TRACE, message, t );
    }

    /*
     * (non-Javadoc)
     * @see kav.util.logging.Logger#trace(java.lang.Object, java.lang.Throwable, java.lang.Object[])
     */
    @Override
    public void trace( Object message, Throwable t, Object ... params )
    {
        if ( this.log.isEnabledFor( Level.TRACE ) )
        {
            String msg = String.format( message.toString(), params );
            this.log.log( FQCN, Level.TRACE, msg, t );
        }
    }

    @Override
    public void tracel7( String key )
    {
        if ( this.log.isEnabledFor( Level.TRACE ) )
        {
            String message = null;

            if ( ( message = this.resourceBundle.getString( key ) ) != null )
            {
                this.log.log( FQCN, Level.TRACE, message, null );
            }
        }
    }

    @Override
    public void tracel7( String key, Object ... params )
    {
        if ( this.log.isEnabledFor( Level.TRACE ) )
        {
            String message = null;

            if ( ( message = this.resourceBundle.getString( key ) ) != null )
            {
                String msg = String.format( message, params );
                this.log.log( FQCN, Level.TRACE, msg, null );
            }
        }
    }

    @Override
    public void tracel7( String key, Throwable t )
    {
        if ( this.log.isEnabledFor( Level.TRACE ) )
        {
            String message = null;

            if ( ( message = this.resourceBundle.getString( key ) ) != null )
            {
                this.log.log( FQCN, Level.TRACE, message, t );
            }
        }
    }

    @Override
    public void tracel7( String key, Throwable t, Object ... params )
    {
        if ( this.log.isEnabledFor( Level.TRACE ) )
        {
            String message = null;

            if ( ( message = this.resourceBundle.getString( key ) ) != null )
            {
                String msg = String.format( message, params );
                this.log.log( FQCN, Level.TRACE, msg, t );
            }
        }
    }

    @Override
    public void error( Object message )
    {
        if ( this.log.isEnabledFor( Level.ERROR ) )
            this.log.log( FQCN, Level.ERROR, message, null );
    }

    @Override
    public void error( Object message, Object ... params )
    {
        if ( this.log.isEnabledFor( Level.ERROR ) )
        {
            String msg = String.format( message.toString(), params );
            this.log.log( FQCN, Level.ERROR, msg, null );
        }
    }

    @Override
    public void error( Object message, Throwable t )
    {
        if ( this.log.isEnabledFor( Level.ERROR ) )
            this.log.log( FQCN, Level.ERROR, message, t );
    }

    @Override
    public void error( Object message, Throwable t, Object ... params )
    {
        if ( this.log.isEnabledFor( Level.ERROR ) )
        {
            String msg = String.format( message.toString(), params );
            this.log.log( FQCN, Level.ERROR, msg, t );
        }
    }

    @Override
    public void errorl7( String key )
    {
        if ( this.log.isEnabledFor( Level.ERROR ) )
        {
            String message = null;

            if ( ( message = this.resourceBundle.getString( key ) ) != null )
            {
                this.log.log( FQCN, Level.ERROR, message, null );
            }
        }
    }

    @Override
    public void errorl7( String key, Object ... params )
    {
        if ( this.log.isEnabledFor( Level.ERROR ) )
        {
            String message = null;

            if ( ( message = this.resourceBundle.getString( key ) ) != null )
            {
                String msg = String.format( message, params );
                this.log.log( FQCN, Level.ERROR, msg, null );
            }
        }
    }

    @Override
    public void errorl7( String key, Throwable t )
    {
        if ( this.log.isEnabledFor( Level.ERROR ) )
        {
            String message = null;

            if ( ( message = this.resourceBundle.getString( key ) ) != null )
            {
                this.log.log( FQCN, Level.ERROR, message, t );
            }
        }
    }

    @Override
    public void errorl7( String key, Throwable t, Object ... params )
    {
        if ( this.log.isEnabledFor( Level.ERROR ) )
        {
            String message = null;

            if ( ( message = this.resourceBundle.getString( key ) ) != null )
            {
                String msg = String.format( message, params );
                this.log.log( FQCN, Level.ERROR, msg, t );
            }
        }
    }

    @Override
    public void fatal( Object message )
    {
        if ( this.log.isEnabledFor( Level.FATAL ) )
            this.log.log( FQCN, Level.FATAL, message, null );
    }

    @Override
    public void fatal( Object message, Object ... params )
    {
        if ( this.log.isEnabledFor( Level.FATAL ) )
        {
            String msg = String.format( message.toString(), params );
            this.log.log( FQCN, Level.FATAL, msg, null );
        }
    }

    @Override
    public void fatal( Object message, Throwable t )
    {
        if ( this.log.isEnabledFor( Level.FATAL ) )
            this.log.log( FQCN, Level.FATAL, message, t );
    }

    @Override
    public void fatal( Object message, Throwable t, Object ... params )
    {
        if ( this.log.isEnabledFor( Level.FATAL ) )
        {
            String msg = String.format( message.toString(), params );
            this.log.log( FQCN, Level.FATAL, msg, t );
        }
    }

    @Override
    public void fatall7( String key )
    {
        if ( this.log.isEnabledFor( Level.FATAL ) )
        {
            String message = null;

            if ( ( message = this.resourceBundle.getString( key ) ) != null )
            {
                this.log.log( FQCN, Level.FATAL, message, null );
            }
        }
    }

    @Override
    public void fatall7( String key, Object ... params )
    {
        if ( this.log.isEnabledFor( Level.FATAL ) )
        {
            String message = null;

            if ( ( message = this.resourceBundle.getString( key ) ) != null )
            {
                String msg = String.format( message, params );
                this.log.log( FQCN, Level.FATAL, msg, null );
            }
        }
    }

    @Override
    public void fatall7( String key, Throwable t )
    {
        if ( this.log.isEnabledFor( Level.FATAL ) )
        {
            String message = null;

            if ( ( message = this.resourceBundle.getString( key ) ) != null )
            {
                this.log.log( FQCN, Level.FATAL, message, t );
            }
        }
    }

    @Override
    public void fatall7( String key, Throwable t, Object ... params )
    {
        if ( this.log.isEnabledFor( Level.FATAL ) )
        {
            String message = null;

            if ( ( message = this.resourceBundle.getString( key ) ) != null )
            {
                String msg = String.format( message, params );
                this.log.log( FQCN, Level.FATAL, msg, t );
            }
        }
    }

    @Override
    public void info( Object message )
    {
        if ( this.log.isEnabledFor( Level.INFO ) )
            this.log.log( FQCN, Level.INFO, message, null );
    }

    @Override
    public void info( Object message, Object ... params )
    {
        if ( this.log.isEnabledFor( Level.INFO ) )
        {
            String msg = String.format( message.toString(), params );
            this.log.log( FQCN, Level.INFO, msg, null );
        }
    }

    @Override
    public void info( Object message, Throwable t )
    {
        if ( this.log.isEnabledFor( Level.INFO ) )
            this.log.log( FQCN, Level.INFO, message, t );
    }

    @Override
    public void info( Object message, Throwable t, Object ... params )
    {
        if ( this.log.isEnabledFor( Level.INFO ) )
        {
            String msg = String.format( message.toString(), params );
            this.log.log( FQCN, Level.INFO, msg, t );
        }
    }

    @Override
    public void infol7( String key )
    {
        if ( this.log.isEnabledFor( Level.INFO ) )
        {
            String message = null;

            if ( ( message = this.resourceBundle.getString( key ) ) != null )
            {
                this.log.log( FQCN, Level.INFO, message, null );
            }
        }
    }

    @Override
    public void infol7( String key, Object ... params )
    {
        if ( this.log.isEnabledFor( Level.INFO ) )
        {
            String message = null;

            if ( ( message = this.resourceBundle.getString( key ) ) != null )
            {
                String msg = String.format( message, params );
                this.log.log( FQCN, Level.INFO, msg, null );
            }
        }
    }

    @Override
    public void infol7( String key, Throwable t )
    {
        if ( this.log.isEnabledFor( Level.INFO ) )
        {
            String message = null;

            if ( ( message = this.resourceBundle.getString( key ) ) != null )
            {
                this.log.log( FQCN, Level.INFO, message, t );
            }
        }
    }

    @Override
    public void infol7( String key, Throwable t, Object ... params )
    {
        if ( this.log.isEnabledFor( Level.INFO ) )
        {
            String message = null;

            if ( ( message = this.resourceBundle.getString( key ) ) != null )
            {
                String msg = String.format( message, params );
                this.log.log( FQCN, Level.INFO, msg, t );
            }
        }
    }

    @Override
    public void warn( Object message )
    {
        if ( this.log.isEnabledFor( Level.WARN ) )
            this.log.log( FQCN, Level.WARN, message, null );
    }

    @Override
    public void warn( Object message, Object ... params )
    {
        if ( this.log.isEnabledFor( Level.WARN ) )
        {
            String msg = String.format( message.toString(), params );
            this.log.log( FQCN, Level.WARN, msg, null );
        }
    }

    @Override
    public void warn( Object message, Throwable t )
    {
        if ( this.log.isEnabledFor( Level.WARN ) )
            this.log.log( FQCN, Level.WARN, message, t );
    }

    @Override
    public void warn( Object message, Throwable t, Object ... params )
    {
        if ( this.log.isEnabledFor( Level.WARN ) )
        {
            String msg = String.format( message.toString(), params );
            this.log.log( FQCN, Level.WARN, msg, t );
        }
    }

    @Override
    public void warnl7( String key )
    {
        if ( this.log.isEnabledFor( Level.WARN ) )
        {
            String message = null;

            if ( ( message = this.resourceBundle.getString( key ) ) != null )
            {
                this.log.log( FQCN, Level.WARN, message, null );
            }
        }
    }

    @Override
    public void warnl7( String key, Object ... params )
    {
        if ( this.log.isEnabledFor( Level.WARN ) )
        {
            String message = null;

            if ( ( message = this.resourceBundle.getString( key ) ) != null )
            {
                String msg = String.format( message, params );
                this.log.log( FQCN, Level.WARN, msg, null );
            }
        }
    }

    @Override
    public void warnl7( String key, Throwable t )
    {
        if ( this.log.isEnabledFor( Level.WARN ) )
        {
            String message = null;

            if ( ( message = this.resourceBundle.getString( key ) ) != null )
            {
                this.log.log( FQCN, Level.WARN, message, t );
            }
        }
    }

    @Override
    public void warnl7( String key, Throwable t, Object ... params )
    {
        if ( this.log.isEnabledFor( Level.WARN ) )
        {
            String message = null;

            if ( ( message = this.resourceBundle.getString( key ) ) != null )
            {
                String msg = String.format( message, params );
                this.log.log( FQCN, Level.WARN, msg, t );
            }
        }
    }

    @Override
    public ResourceBundle getResourceBundle()
    {
        return this.resourceBundle;
    }

    @Override
    public void setResourceBundle( ResourceBundle bundle )
    {
        this.resourceBundle = bundle;
        this.log.setResourceBundle( bundle );
    }
}
